package com.example.tool.erp.demo;

import com.example.tool.erp.sdk.Client;
import com.example.tool.erp.sdk.Pager;
import com.example.tool.erp.sdk.WdtErpException;
import com.example.tool.erp.sdk.api.finance.SettleAPI;
import com.example.tool.erp.sdk.api.finance.dto.PurchaseSettleSearchRequest;
import com.example.tool.erp.sdk.api.finance.dto.PurchaseSettleSearchResponse;
import com.example.tool.erp.sdk.api.finance.dto.PurchaseSettleUploadRequest;
import com.example.tool.erp.sdk.api.finance.dto.PurchaseSettleUploadResponse;
import com.example.tool.erp.sdk.impl.ApiFactory;
import com.example.tool.erp.sdk.impl.DefaultClient;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class Settle
{
	public static void main(String[] args) throws WdtErpException
	{
		Client client = DefaultClient.get("wdterp30", "http://172.172.1.62:30000/", "xyx_api",
				"e8866c1681dacc9488a19c89991791b8:8f5800dab84b7a8d614e92cba739032c");
		SettleAPI api = ApiFactory.get(client, SettleAPI.class);

//		testSearchPurchaseSettle(api);
		testUploadPurchaseSettle(api);
	}

	private static void testSearchPurchaseSettle(SettleAPI api) throws WdtErpException
	{
		int pageSize = 5;
		Pager pager = new Pager(pageSize, 0, true);
		PurchaseSettleSearchRequest request = new PurchaseSettleSearchRequest();
		request.setCreateFrom("2022-03-01");
		request.setCreateTo("2022-03-29");

		PurchaseSettleSearchResponse response = api.searchPurchaseSettle(request, pager);
		Integer total = response.getTotal();
		if (null == total || pageSize >= total)
		{
			System.out.println("处理数据");
			return;
		}

		int totalPage = (total % pageSize == 0 ? total / pageSize : total / pageSize + 1) - 1;
		pager.setCalcTotal(false); //后续翻页不需要计算总条数, 可以大大减少请求时间
		for (int i = totalPage; i >= 0; i--)// 从后向前翻页
		{
			pager.setPageNo(i);
			System.out.print("pager: page_size:" + pageSize + "  page_no: " + i + "  ");
			response = api.searchPurchaseSettle(request, pager);
			System.out.println(response.getOrderList());
		}
	}

	private static void testUploadPurchaseSettle(SettleAPI api) throws WdtErpException
	{
		PurchaseSettleUploadRequest.SettleOrder settleOrder = new PurchaseSettleUploadRequest.SettleOrder();
		settleOrder.setPurchaseNo("CG2305300003");

		List<PurchaseSettleUploadRequest.Detail> detailList = new ArrayList<>();
		PurchaseSettleUploadRequest.Detail detail = new PurchaseSettleUploadRequest.Detail();
		detailList.add(detail);

		detail.setSpecNo("gy01");
		detail.setSettleNum(new BigDecimal("1"));

		PurchaseSettleUploadResponse response = api.uploadPurchaseSettle(settleOrder, detailList);
		System.out.println("结算单号: "+response.getOrderNo());
	}
}
